package edu.asu.sapa.sas;

import java.util.*;

public class SASOperator {
	protected String name;
	static protected int numOperators = 0;
	protected int id;
	protected ArrayList<Prevail> prevailList;
	
	/** A varID, valueID pair (lookup on varID) -- assumes one value per variable */
	protected HashMap<Integer, Integer> prevailValues = new HashMap<Integer, Integer>();

	protected ArrayList<Effect> effectList;
	
	protected ArrayList<Variable> variableList;

	float cost = 1.0f;
	
	/** Note for the prevail conditions that we assume one value per variable exists. */
	public SASOperator(String name, ArrayList<Prevail> prevailList, ArrayList<Effect> effectList) {
		this.name = name;
		this.prevailList = prevailList;
		this.effectList = effectList;

		id = numOperators++;

		for (Condition cond : prevailList) {
			prevailValues.put(cond.getVarID(), cond.getValueID());
		}
	}
	
	public void setCost(float cost) {
		this.cost = cost;
	}
	
	public float getCost() {
		return cost;
	}
	
	public ArrayList<Effect> getEffectList() {
		return effectList;
	}
	
	public ArrayList<Prevail> getPrevailList() {
		return prevailList;
	}
		
	/** A list of variables that this operator has an effect or prevails on */
	public ArrayList<Variable> getVariables() {
		if (variableList != null) return variableList;
		HashSet<Variable> vars = new HashSet<Variable>();

		for (Effect e : effectList) {
			vars.add(e.getVariable());
		}

		for (Prevail p : prevailList) {
			vars.add(p.getVariable());
		}
		
		variableList = new ArrayList<Variable>(vars);
		return variableList;
	}
	
	public int getID() {
		return id;
	}
	
	public int hashCode() {
		Integer i = new Integer(id);
		return i.hashCode();
	}
	
	public boolean equals(Object obj) {
		if (!(obj instanceof SASOperator)) return false;
		SASOperator op = (SASOperator)obj;
		
		if (op.id == id) return true;
		return false;
	}

	@Override
	public String toString() {
		StringBuilder st = new StringBuilder("OpID: " + id);
		
		for (Prevail prevail : prevailList) {
			st.append(prevail).append(' ');
		}
		
		return st.toString();
	}

	public String getName() {
		return name;
	}
}
